package uiLayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import uiLayer.util.MainPanelTemplate;
import modelLayer.Class;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import controlLayer.ClassCtr;

public class ClassPanel extends MainPanelTemplate {
	private static final long serialVersionUID = 1L;
	
	private JList<Class> classJList;
	private DefaultListModel<Class> classListModel;
	private ClassCtr classCtr;
	private List<Class> classList;

	
	/**
	 * Create the panel.
	 */
	public ClassPanel(){
		
		try {
			classCtr = new ClassCtr();
		} catch (Exception e) {
			//new StandardDialog().showErrorMessage("Check database connection");
		}
		super.getScrollPane().setBorder(new TitledBorder("Klasser"));
		super.getSplitPane().setRightComponent(new JPanel()); // sets the right component on the splitPane as an empty JPanel
		
		
		classListModel = new DefaultListModel<>();
		classJList = new JList<>(classListModel);
		initiateClassList();
		classJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		
		JButton btnEditTeachingBlocks = new JButton("Rediger fag");
		btnEditTeachingBlocks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editTeachingBlocks();
			}
		});
		super.getButtonPanel().add(btnEditTeachingBlocks);
		
		super.getScrollPane().setViewportView(classJList);
	}
	
	private void initiateClassList() {
		classList = new ArrayList<Class>();
		try {
			classList = classCtr.getAll(true);
			if (!classList.isEmpty()){
				for(Class cl : classList) {
					classListModel.addElement(cl);
				}
			}
			else {
				new StandardDialog().objectNotFoundDialog("List af klasser ");
			}
		}
		catch (Exception e) {
			new StandardDialog().showErrorMessage("Check database connection");
		}
	}
	


	private void editTeachingBlocks() {
		Class cl = classJList.getSelectedValue();
		if (cl != null) {
			super.getSplitPane().setRightComponent(new TeachingBlockEditPanel(cl));
		}
		else {
			new StandardDialog().noSelectionDialog("klasse ");
		}
	}

	@Override
	protected void doPrint() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doEdit() {
		Class cl = classJList.getSelectedValue();
		if(cl != null) {
			super.getSplitPane().setRightComponent(new ClassEditPanel(this, classCtr, cl));
			}
			else {
			new StandardDialog().showErrorMessage("Du skal v\u00E6lge en klasse for at kunne redigere den.");
		}
			
	}

	@Override
	protected void doNew() {
		super.getSplitPane().setRightComponent(new ClassEditPanel(this, classCtr, null));
		
	}
	
	
	@Override
	protected void doDelete() {
		Class cl = classJList.getSelectedValue();
		if(cl != null) {
			int n = new StandardDialog().deleteDialog("klasse", cl.getClassName());
			if(n == JOptionPane.YES_NO_OPTION) {
				try {
					classCtr.removeClass(cl);
					classListModel.removeElement(cl);
					
				} catch (SQLException e) {
					new StandardDialog().showErrorMessage(e.getMessage());
					//new StandardDialog().showErrorMessage("I klassen " + cl.getClassName() + " er der registreret elever. \n Det er ikke muligt at slette klasse uden at ophæve associationen først.");
				}
			}
			
		}else {
			new StandardDialog().noSelectionDialog("klasse ");
		}
	}

	public DefaultListModel<Class> getClassListModel() {
		return classListModel;
	}

}
